package com.biz.primus.ms.commodity.dao.specification;

import com.biz.primus.ms.commodity.dao.po.Brand;
import com.google.common.collect.Lists;
import org.springframework.data.jpa.domain.Specification;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.List;

/**
 * Created by aaa on 2017/8/3.
 */
public class UnclassifiedBrandSpecification implements Specification<Brand> {
    @Override
    public Predicate toPredicate(Root<Brand> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
        List<Predicate> predicates = Lists.newArrayList();
        // 查询未删除的
        predicates.add(cb.equal(root.get("deleteFlag").as(Boolean.class), Boolean.FALSE));
        predicates.add(cb.isEmpty(root.get("categories").as(List.class)));
        query.where(predicates.toArray(new Predicate[predicates.size()]));
        return query.getRestriction();
    }
}
